﻿/***************************************************************************/
/// <summary>
///  <title>SLIC Email Model</title>
///  <description>Email Model for Emails Details</description>
///  <copyRight>Copyright (c) 2010</copyRight>
///  <company>IronOne Technologies (Pvt)Ltd</company>
///  <createdOn>2011-08-01</createdOn>
///  <author></author>
///  <modification>
///     <modifiedBy></modifiedBy>
///      <modifiedDate></modifiedDate>
///     <description></description>
///  </modification>
///
/// </summary>
/***************************************************************************/


using System;
using System.Collections.Generic;
using System.Linq;
/***************************************************************************/
/// <summary>
///  <title>SLIC EmailModel</title>
///  <description>Email Model for Email details</description>
///  <copyRight>Copyright (c) 2010</copyRight>
///  <company>IronOne Technologies (Pvt)Ltd</company>
///  <createdOn>2011-08-01</createdOn>
///  <author></author>
///  <modification>
///     <modifiedBy></modifiedBy>
///      <modifiedDate></modifiedDate>
///     <description></description>
///  </modification>
///
/// </summary>
/***************************************************************************/

using System.Web;
using System.Net.Mail;
using System.Collections.Specialized;
using System.Net;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI;

using SLIC.Controllers;
using SLIC.Models.Enums;

using log4net;

namespace SLIC.Models.Job
{
    public class Email
    {
        protected static readonly ILog log = LogManager.GetLogger("root");
        public EntityModel.Job jobEntity { get; set; }
        public System.Net.Mail.MailMessage mailMsg { get; set; }

        public Email(EntityModel.Job job)
        {
            jobEntity = job;
        }

        public Email(System.Net.Mail.MailMessage jobMail)
        {
            mailMsg = jobMail;
        }
        public void StartMailThread()
        {
            try
            {
                log.Info(LogPoint.Entry.ToString() + ",StartMailThread,Subject=" + mailMsg.Subject);
                SmtpClient client = new SmtpClient();
                client.Timeout = 300000;//default is  100,000 - 100 seconds
                try
                {
                    //Set from web config
                    client.Timeout = Convert.ToInt32(ConfigurationManager.AppSettings["SMTP_Cleint_Timeout"]);
                }
                catch (Exception)   
                {                    
                  //  throw;
                }
               
                client.Host = ConfigurationManager.AppSettings["SMTP_HOST"].ToString(); //Set your smtp host address
                client.Port = int.Parse(ConfigurationManager.AppSettings["SMTP_PORT"].ToString()); // Set your smtp port address
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["MAIL_SERVER_USERNAME"].ToString(), ConfigurationManager.AppSettings["MAIL_SERVER_PASSWORD"].ToString()); //account name and password
                client.EnableSsl = true; // Set SSL = true             
                client.Send(mailMsg);
                log.Info(LogPoint.Success.ToString() + ",StartMailThread,Subject=" + mailMsg.Subject);

            }
            catch (Exception ex)
            {
                log.Error(LogPoint.Failure.ToString() + ",StartMailThread,Subject=" + mailMsg.Subject + "," + ex.Message);
              //  throw;
            }
            mailMsg.Dispose();
        }
    }
}